package org.jeecg.modules.basedata.service.impl;


import org.jeecg.common.constant.CommonConstant;
import org.jeecg.common.util.SsoUtil;
import org.jeecg.common.util.StringUtils;
import org.jeecg.entity.basedata.BaseUnit;
import org.jeecg.enums.base.DepartPropertyEnum;
import org.jeecg.modules.basedata.mapper.BaseProjectMapper;
import org.jeecg.modules.basedata.mapper.BaseUnitMapper;
import org.jeecg.modules.basedata.service.IBaseProjectService;
import org.jeecg.modules.basedata.service.IBaseUnitService;
import org.jeecg.modules.system.model.DepartIdModel;
import org.jeecg.modules.system.service.ISysUserRoleService;
import org.jeecg.modules.system.util.FindsDepartsChildrenUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.jeecg.modules.system.model.SysDepartTreeModel;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.jeecg.modules.system.entity.SysDepart;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Description: 单元管理
 * @Author: jeecg-boot
 * @Date:   2019-07-03
 * @Version: V1.0
 */
@Service
public class BaseUnitServiceImpl extends ServiceImpl<BaseUnitMapper, BaseUnit> implements IBaseUnitService {

    @Autowired
    private BaseProjectMapper baseProjectMapper;
    @Autowired
    private BaseUnitMapper baseUnitMapper;
    @Autowired
    private IBaseProjectService baseProjectService;
    @Autowired
    private ISysUserRoleService sysUserRoleService;

    @Override
    public List<SysDepartTreeModel> queryUnitTreeList(String menuPath) {
        List<SysDepartTreeModel> list = new ArrayList<>();
        if (StringUtils.isNotBlank(menuPath)){
            //根据权限加载
            List<SysDepart> departTreeList = sysUserRoleService.queryDepartTreeList(menuPath, SsoUtil.getLoginUser().getId(), CommonConstant.DEPART_WHERE_CO);
            List<String> departIdList = new ArrayList<>();
            Map<String,SysDepart> departTreeMap= new HashMap<>();
            List<SysDepartTreeModel> branchList = new ArrayList<>();
            for (SysDepart departTree: departTreeList){
                departTreeMap.put(departTree.getId(),departTree);
                departIdList.add(departTree.getId());
                if (departTree.getType().equals(DepartPropertyEnum.DEPART_PROPERTY_OFFICE.getCode())) continue;
                SysDepartTreeModel branch = new SysDepartTreeModel(departTree);
                branchList.add(branch);
            }
            List<DepartIdModel> idList = new ArrayList<>();
            for (SysDepart depart:departTreeList){
                if (depart.getType().equals(DepartPropertyEnum.DEPART_PROPERTY_DEPARTMENT.getCode())) continue;
                if (depart.getType().equals(DepartPropertyEnum.DEPART_PROPERTY_OFFICE.getCode())) continue;
                if ((StringUtils.isBlank(depart.getParentId())) || (StringUtils.isNotBlank(depart.getParentId()) && !departTreeMap.containsKey(depart.getParentId()))) {
                    SysDepartTreeModel branch = new SysDepartTreeModel(depart);
                    list.add(branch);
                    DepartIdModel departIdModel = new DepartIdModel().convert(branch);
                    idList.add(departIdModel);
                }
            }
            if (departTreeList.size() > 0){
                List<SysDepart> projectTreeList =  sysUserRoleService.queryProjectTreeListByDepartId(departIdList);
                for (SysDepart project:projectTreeList){
                    SysDepartTreeModel branch = new SysDepartTreeModel(project);
                    List<SysDepartTreeModel> listAll= baseProjectMapper.queryUnitTreeList(project.getId());
                    if(listAll != null && listAll.size() > 0){
                        for(SysDepartTreeModel room:listAll){
                            room.setKey(room.getId());
                            room.setValue(room.getId());
                            room.setIsLeaf(true);
                            room.setTitle(room.getDepartName());
                        }
                        branchList.addAll(listAll);
                    }
                    branchList.add(branch);
                }
                FindsDepartsChildrenUtil.getGrandChildren(list,branchList,idList);
                FindsDepartsChildrenUtil.setEmptyChildrenAsNull(list);
            }
        }
        return list;
    }

    @Override
    public List<BaseUnit> queryUnitList(String floorId) {
        List<BaseUnit> list = baseUnitMapper.queryUnitList(floorId);
        return list;
    }

    @Override
    public List<SysDepartTreeModel> queryRoomCusTenantTreeList(String type,String roomStatus,String menuPath) {
        List<SysDepartTreeModel> list = new ArrayList<>();
        if (StringUtils.isNotBlank(menuPath)){
            //根据权限加载
            List<SysDepart> departTreeList = sysUserRoleService.queryDepartTreeList(menuPath, SsoUtil.getLoginUser().getId(), CommonConstant.DEPART_WHERE_CO);
            List<String> departIdList = new ArrayList<>();
            Map<String,SysDepart> departTreeMap= new HashMap<>();
            List<SysDepartTreeModel> branchList = new ArrayList<>();
            for (SysDepart departTree: departTreeList){
                departTreeMap.put(departTree.getId(),departTree);
                departIdList.add(departTree.getId());
                if (departTree.getType().equals(DepartPropertyEnum.DEPART_PROPERTY_OFFICE.getCode())) continue;
                SysDepartTreeModel branch = new SysDepartTreeModel(departTree);
                branchList.add(branch);
            }
            List<DepartIdModel> idList = new ArrayList<>();
            for (SysDepart depart:departTreeList){
                if (depart.getType().equals(DepartPropertyEnum.DEPART_PROPERTY_DEPARTMENT.getCode())) continue;
                if (depart.getType().equals(DepartPropertyEnum.DEPART_PROPERTY_OFFICE.getCode())) continue;
                if ((StringUtils.isBlank(depart.getParentId())) || (StringUtils.isNotBlank(depart.getParentId()) && !departTreeMap.containsKey(depart.getParentId()))) {
                    SysDepartTreeModel branch = new SysDepartTreeModel(depart);
                    list.add(branch);
                    DepartIdModel departIdModel = new DepartIdModel().convert(branch);
                    idList.add(departIdModel);
                }
            }
            if (departTreeList.size() > 0){
                List<SysDepart> projectTreeList =  sysUserRoleService.queryProjectTreeListByDepartId(departIdList);
                for (SysDepart project:projectTreeList){
                    SysDepartTreeModel branch = new SysDepartTreeModel(project);
                    List<SysDepartTreeModel> listAll= baseProjectMapper.queryUnitTreeList(project.getId());
                    if(listAll != null && listAll.size() > 0){
                    for(SysDepartTreeModel treeModel: listAll){/*
                        List<SysDepartTreeModel> roomList = null;
                        if("1".equals(roomStatus) || StringUtils.isEmpty(roomStatus)) {
                            if("1".equals(type)){//租户
                                roomList =  baseProjectMapper.queryTenantList(treeModel.getId());
                            }
                            if("2".equals(type)){//业主
                                roomList =  baseProjectMapper.queryCustomerList(treeModel.getId());
                            }
                        }else {
                            roomList = baseProjectMapper.queryOutCustomer(treeModel.getId());
                        }
                        if(roomList != null && roomList.size() > 0){
                            for(SysDepartTreeModel room:roomList){
                                room.setKey(room.getId());
                                room.setValue(room.getId());
                                room.setIsLeaf(true);
                                room.setTitle(room.getDepartName());
                            }
                            branchList.addAll(roomList);
                        }
                    */}
                        branchList.addAll(listAll);
                    }
                    branchList.add(branch);
                }
                FindsDepartsChildrenUtil.getGrandChildren(list,branchList,idList);
                FindsDepartsChildrenUtil.setEmptyChildrenAsNull(list);
            }
        }
        return list;
    }
}
